package com.kinyx.framework.bean;

import java.io.File;

import org.junit.Assert;
import org.junit.Test;

import com.kinyx.framework.Configs;
import com.kinyx.framework.file.KPathUtils;
import com.kinyx.framework.image.KImageUtils;
import com.kinyx.framework.security.KBASE64;

public class KImageInfo_Tester_ {

	final String rootUrl = Configs.fileServer_rootUrl;
	final String _rootPath = "/file";
	final String rootPath = KPathUtils.target() + "/test-classes" + this._rootPath;
	final String category = "/category";
	final String fileName = "/test.jpg";
	final String relativePath = this.category + this.fileName;
	final String absolutePath = this.rootPath + this.relativePath;
	final String base64 = KBASE64.encode(KImageUtils.readBytes(new File(this.rootPath + this.relativePath)), null);
	final String url = this.rootUrl + this.relativePath;

	private void check(final KImageInfo image, final String absolutePath, final String base64, final String relativePath, final String relativePath2, final String url) {
		Assert.assertEquals(absolutePath, image.getAbsolutePath());
		Assert.assertEquals(base64, image.getBase64());
		Assert.assertEquals(relativePath, image.getRelativePath());
		Assert.assertEquals(relativePath2, image.getRelativePath2());
		Assert.assertEquals(url, image.getUrl());
	}

	@Test
	public void test() {
		this.其它服务器地址(this.rootUrl, this.rootPath, this.category);
		this.本服务器地址(this.rootUrl, this.rootPath, this.category);
		this.全路径(this.rootUrl, this.rootPath, this.category);
		this.相对路径(this.rootUrl, this.rootPath, this.category);
		this.BASE64();
	}

	public void 其它服务器地址(final String rootUrl, final String rootPath, final String category) {
		final String str = "ftp://www.kinyx.com" + this.relativePath;
		final KImageInfo image = KImageInfo.of(this.rootUrl, this.rootPath, this.category).with(str).create(false);
		this.check(image, null, null, null, null, str);
	}

	public void 本服务器地址(final String rootUrl, final String rootPath, final String category) {
		final String str = this.url;
		final KImageInfo image = KImageInfo.of(this.rootUrl, this.rootPath, this.category).with(str).create(false);
		this.check(image, this.absolutePath, this.base64, this.relativePath, this.fileName, this.url);
	}

	public void 全路径(final String rootUrl, final String rootPath, final String category) {
		final String str = this.absolutePath;
		final KImageInfo image = KImageInfo.of(this.rootUrl, this.rootPath, this.category).with(str).create(false);
		this.check(image, this.absolutePath, this.base64, this.relativePath, this.fileName, this.url);
	}

	public void 相对路径(final String rootUrl, final String rootPath, final String category) {
		final String str = this.relativePath;
		final KImageInfo image = KImageInfo.of(this.rootUrl, this.rootPath, this.category).with(str).create(false);
		this.check(image, this.absolutePath, this.base64, this.relativePath, this.fileName, this.url);
	}

	public void BASE64() {
		final String str = this.base64;
		final KImageInfo image = KImageInfo.of(this.rootUrl, this.rootPath, this.category).with(str).create(false);
		Assert.assertTrue(image.getAbsolutePath().startsWith(this.rootPath + this.category));
		Assert.assertEquals(this.base64, image.getBase64());
		Assert.assertTrue(image.getRelativePath().startsWith(this.category));
		Assert.assertTrue(image.getRelativePath2().equals(image.getRelativePath().substring(this.category.length())));
		Assert.assertTrue(image.getUrl().startsWith(this.rootUrl + this.category));
	}

}
